<template>
	<view class="set-password-container">
		<view class="password-form">
			<view class="form-title">设置密码</view>
			
			<view class="form-item">
				<text class="label">手机号</text>
				<input type="text" disabled :value="phone" placeholder="手机号" />
			</view>
			
			<view class="form-item">
				<text class="label">设置密码</text>
				<input 
					type="password" 
					v-model="password" 
					placeholder="请输入密码" 
					maxlength="20" 
					password="true" 
				/>
			</view>
			
			<view class="form-item">
				<text class="label">确认密码</text>
				<input 
					type="password" 
					v-model="confirmPassword" 
					placeholder="请再次输入密码" 
					maxlength="20" 
					password="true" 
				/>
			</view>
			
			<view class="tips">
				<text>密码长度为6-20位，建议使用字母、数字和符号组合</text>
			</view>
			
			<button class="submit-btn" @click="submitPassword">确认设置</button>
		</view>
	</view>
</template>

<script>
import api from '../../utils/api.js';
import userManager from '../../utils/user.js';

export default {
	data() {
		return {
			phone: '',
			password: '',
			confirmPassword: ''
		}
	},
	onLoad() {
		// 从全局用户信息获取手机号
		this.phone = userManager.getUserPhone();
		
		// 如果没有获取到手机号，则返回上一页
		if (!this.phone) {
			uni.showToast({
				title: '请先登录',
				icon: 'none'
			});
			
			setTimeout(() => {
				uni.navigateBack();
			}, 1500);
		}
	},
	methods: {
		// 提交密码设置
		submitPassword() {
			// 检查密码是否为空
			if (!this.password) {
				uni.showToast({
					title: '请输入密码',
					icon: 'none'
				});
				return;
			}
			
			// 检查密码长度
			if (this.password.length < 6 || this.password.length > 20) {
				uni.showToast({
					title: '密码长度应为6-20位',
					icon: 'none'
				});
				return;
			}
			
			// 检查两次密码是否一致
			if (this.password !== this.confirmPassword) {
				uni.showToast({
					title: '两次输入的密码不一致',
					icon: 'none'
				});
				return;
			}
			
			// 调用设置密码接口
			api.user.registerPassword(this.phone, this.password).then(res => {
				if (res.data && res.data.code === 200) {
					uni.showToast({
						title: '密码设置成功',
						icon: 'success'
					});
					
					// 更新本地存储的用户信息，标记用户已设置密码
					userManager.updateUserInfo({
						hasPassword: true
					});
					
					// 设置成功后清除登录信息并跳转到登录页面
					setTimeout(() => {
						// 清除用户登录信息
						userManager.clearUserInfo();
						
						// 跳转到登录页面
						uni.reLaunch({
							url: '/pages/login/login'
						});
					}, 1500);
				}
			}).catch(() => {
				// 错误处理已在 request.js 中统一处理
			});
		}
	}
}
</script>

<style lang="scss">
.set-password-container {
	padding: 30px;
	display: flex;
	flex-direction: column;
	align-items: center;
	height: 100vh;
	background-color: #f5f5f5;
}

.password-form {
	width: 100%;
	max-width: 600rpx;
	padding: 40rpx;
	background-color: #fff;
	border-radius: 12rpx;
	box-shadow: 0 0 20rpx rgba(0, 0, 0, 0.1);
	margin-top: 100rpx;
}

.form-title {
	font-size: 36rpx;
	font-weight: bold;
	text-align: center;
	margin-bottom: 60rpx;
	color: #333;
}

.form-item {
	margin-bottom: 30rpx;
}

.label {
	font-size: 28rpx;
	color: #333;
	margin-bottom: 10rpx;
	display: block;
}

input {
	width: 100%;
	height: 80rpx;
	background-color: #f5f5f5;
	border-radius: 8rpx;
	padding: 0 20rpx;
	font-size: 28rpx;
	box-sizing: border-box;
}

input[disabled] {
	background-color: #f0f0f0;
	color: #999;
}

.tips {
	font-size: 24rpx;
	color: #999;
	margin-bottom: 40rpx;
	padding: 0 10rpx;
}

.submit-btn {
	width: 100%;
	height: 90rpx;
	line-height: 90rpx;
	background-color: #007aff;
	color: #fff;
	font-size: 32rpx;
	border-radius: 45rpx;
}
</style> 